package com.hd.trans.network.repository

import com.alibaba.fastjson.JSONArray
import com.hd.trans.extension.charAt
import com.hd.trans.network.ApiServiceManager
import com.hd.trans.utils.StringUtils

object RomaRepository {

    private fun splitString(text: String):Array<String>{
        return StringUtils.splitStringByLength(text, 1000)
    }

    /**
     * 是否为控制字符\n\r\t
     */
    private fun isControlChar(c: Char?): Boolean {
        return c == '\n' || c == '\r' || c == '\t'
    }

    /**
     * 韩语转罗马音
     * @param _text 文本
     */
    suspend fun korean2roma(_text: String): String {
        val builder = StringBuilder()
        splitString(_text).forEach { text->
            val result = ApiServiceManager.romaService.korean2roma(text)
            builder.append(result)
        }

        val result = builder.toString().trim()

        if (result == _text.trim()){
            return ""
        }

        return result
    }

    /**
     * 粤语转拼音
     * @param _text 文本
     */
    suspend fun cantonese2roma(_text: String): String {
        val builder = StringBuilder()
        splitString(_text).forEach { text->
            val result = ApiServiceManager.romaService.cantonese2roma(text)
            // ["nei5", "hou3", "nei5", "hek3", "faan6", "liu5", ["maa1", "maa3"], null]
            try {
                val resultArray = JSONArray.parseArray(result)
                for (i in 0 until resultArray.size){
                    val word:Any? = resultArray[i]
                    var pinyin:String? = null
                    if (word != null) {
                        if (word is JSONArray) {
                            if (word.isNotEmpty()) {
                                pinyin = word.getString(0)
                            }
                        } else {
                            pinyin = word.toString()
                        }
                    }

                    if (pinyin.isNullOrEmpty()){
                        val orgChar = text.charAt(i,' ')
                        if (isControlChar(orgChar)){
                            builder.append(orgChar)
                        }
                    }
                    else{
                        if (builder.isNotEmpty()){
                            builder.append(' ')
                        }
                        builder.append(pinyin)
                    }
                }

            }catch (e:Exception) {
                e.printStackTrace()
            }
        }

        val result = builder.toString().trim()

        if (result == _text.trim()){
            return ""
        }

        return result
    }

    /**
     * 日语转罗马音
     * @param _text 文本
     */
    suspend fun japanese2roma(_text: String): String {
        val builder = StringBuilder()
        splitString(_text).forEach { text->
            val result = ApiServiceManager.romaService.japanese2roma(text)
            //[{"orig":"今夜","hira":"こんや","kana":"コンヤ","hepburn":"konya","kunrei":"konya","passport":"konya"},{"orig":"は","hira":"は","kana":"ハ","hepburn":"ha","kunrei":"ha","passport":"ha"},{"orig":"月","hira":"がつ","kana":"ガツ","hepburn":"gatsu","kunrei":"gatu","passport":"gatsu"},{"orig":"が","hira":"が","kana":"ガ","hepburn":"ga","kunrei":"ga","passport":"ga"},{"orig":"绮","hira":"","kana":"","hepburn":"","kunrei":"","passport":""},{"orig":"丽","hira":"","kana":"","hepburn":"","kunrei":"","passport":""},{"orig":"ですね","hira":"ですね","kana":"デスネ","hepburn":"desune","kunrei":"desune","passport":"desune"}]
            try {
                val resultArray = JSONArray.parseArray(result)
                for (i in 0 until resultArray.size){
                    val word = resultArray.getJSONObject(i)

                    val pinyin:String? = word.getString("hepburn")

                    if (pinyin.isNullOrEmpty()){
                        builder.append(word.getString("orig"))
                    }
                    else{
                        builder.append(pinyin)
                    }
                }

            }catch (e:Exception) {
                e.printStackTrace()
            }
        }

        val result = builder.toString().trim()

        if (result == _text.trim()){
            return ""
        }

        return result
    }

    /**
     * 汉语转拼音
     * @param _text 文本
     */
    suspend fun chinese2pinyin(_text: String): String {
        val builder = StringBuilder()

        splitString(_text).forEach { text->
            val result = ApiServiceManager.romaService.chinese2roma(text)
            //[["nǐ"],["hǎo"],["ma"]]
            try {
                val resultArray = JSONArray.parseArray(result)
                for (i in 0 until resultArray.size){
                    val wordArray = resultArray.getJSONArray(i) ?: JSONArray()
                    var pinyin:String? = null
                    if (wordArray.isNotEmpty()){
                        pinyin = wordArray.getString(0)
                    }

                    if (pinyin.isNullOrEmpty()){
                        builder.append(text.charAt(i,' '))
                    }
                    else{
                        if (builder.isNotEmpty()){
                            builder.append(' ')
                        }
                        builder.append(pinyin)
                    }
                }
            }catch (e:Exception){
                e.printStackTrace()
            }
        }

        val result = builder.toString().trim()

        if (result == _text.trim()){
            return ""
        }

        return result
    }
}